package controller.admin;

import com.jfoenix.controls.JFXComboBox;
import controller.Items.request;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import sql.JDBC_T;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.MessageFormat;

public class AdminAddStuController {
//    @FXML
//    private TableView adm_stu_regTable;
//    @FXML
//    private Button adm_btn_search;
//    @FXML
//    private  TableColumn<request,Button> adm_stu_reg_result;
    @FXML
    private TextField adm_stu_add_tf_sid;
    @FXML
    private TextField adm_stu_add_tf_sname;
    @FXML
    private RadioButton adm_stu_add_rb_male;
    @FXML
    private TextField adm_stu_add_tf_dnum;
    @FXML
    private TextField adm_stu_add_cmb_class;
    @FXML
    private TextField adm_stu_add_tf_rnum;
    private Alert alert;

//    public void initialize() throws SQLException {
//        String sql = "SELECT request.username,request.birth,request.address,request.gender,request.identity\n" +
//                "FROM request\n" +
//                "ORDER BY request.ID;";
//        JDBC_T jdbc_t = new JDBC_T();
//        String username,birth,address,gender,identity;
//        ObservableList<request> requests = FXCollections.observableArrayList();
//        if(jdbc_t.is_conn()){
//            PreparedStatement preparedStatement = jdbc_t.getConn().prepareStatement(sql);
//            ResultSet resultSet = preparedStatement.executeQuery();
//            while (resultSet.next()){
//                username = resultSet.getString("username");
//                birth = resultSet.getString("birth");
//                gender = resultSet.getString("gender");
//                address = resultSet.getString("address");
//                identity = resultSet.getString("identity");
//                requests.add(new request(username,birth,address,gender,identity));
//            }
//            ObservableList<TableColumn> cols=adm_stu_regTable.getColumns();
//            TableColumn NAME= cols.get(0);
//            TableColumn BIRTH = cols.get(1);
//            TableColumn  ADDRSS= cols.get(2);
//            TableColumn  GENDER= cols.get(3);
//            TableColumn  IDENTITY = cols.get(4);
//            NAME.setCellValueFactory(new PropertyValueFactory<>("username"));
//            BIRTH.setCellValueFactory(new PropertyValueFactory<>("birth"));
//            ADDRSS.setCellValueFactory(new PropertyValueFactory<>("address"));
//            GENDER.setCellValueFactory(new PropertyValueFactory<>("gender"));
//            IDENTITY.setCellValueFactory(new PropertyValueFactory<>("identity"));
////            adm_stu_reg_result.setCellValueFactory(
////                    cellData-> (ObservableValue<Button>) cellData.getValue().button
////            );
//            adm_stu_regTable.setItems(requests);
//        }
//    }
    public void register()throws Exception{

            String sid,sname,gender,sclass,sbnum,srnum;
            sid = adm_stu_add_tf_sid.getText();
            sname = adm_stu_add_tf_sname.getText();
            if(adm_stu_add_rb_male.isSelected()){
                gender = "男";
            }else {
                gender = "女";
            }
            sclass = adm_stu_add_cmb_class.getText();
            sbnum = adm_stu_add_tf_dnum.getText();
            srnum = adm_stu_add_tf_rnum.getText();
            String sql =MessageFormat.format("Insert into   stuinfo values ({0}{1}{0},{0}{2}{0},{0}{3}{0},{0}{4}{0},{0}{5}{0},{0}{6}{0},{0}{7}{0},{0}{8}{0})",'"',sid,sname,"123123",gender,"00"+sclass.charAt(0),sbnum,srnum,sclass);
            JDBC_T jdbc_t = new JDBC_T();
            if(jdbc_t.is_conn()){
                try{
                    PreparedStatement preparedStatement = jdbc_t.getConn().prepareStatement(sql);
                    System.out.println(sql);
                    preparedStatement.execute(sql);
                    alert= new Alert(Alert.AlertType.INFORMATION);
                    alert.setContentText("注册成功!!!");
                    alert.showAndWait();
                }catch (SQLException sqlException){
                    alert= new Alert(Alert.AlertType.WARNING);
                    alert.setContentText("插入数据表失败!!!请检查后重新插入");
                    alert.showAndWait();
                }

            }

    }

}
